package com.example.service;

import com.example.domain.Scholarship;
import com.example.mapper.ScholarshipMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 奖学金服务层
 */
@Service
public class ScholarshipService {

    @Autowired
    private ScholarshipMapper scholarshipMapper;

    public PageInfo<Map> selectPageList(Scholarship scholarship) {
        PageHelper.startPage(scholarship.getPageNum(), 10);
        List<Map> list = scholarshipMapper.selectList(scholarship.getParams());
        return new PageInfo<Map>(list);
    }


    public List<Map> selectListByAcademicYear(String academicYear, Integer id) {
        Map<String, Object> params = new HashMap<>();
        params.put("academicYear", academicYear);
        params.put("id", id);
        return scholarshipMapper.selectList(params);
    }

    public void save(Scholarship scholarship) {

        if (scholarship.getScholarshipId() == null) {
            scholarship.setCreateTime(new Date());
            scholarshipMapper.insert(scholarship);
        } else {
            scholarshipMapper.updateByPrimaryKey(scholarship);
        }
    }

    public Scholarship selectById(Long scholarshipId) {

        return this.scholarshipMapper.selectByPrimaryKey(scholarshipId);
    }

    public void deleteById(Long id) {
        this.scholarshipMapper.deleteByPrimaryKey(id);
    }
}
